package com.aliyun.heiyu.powermaster.server.rotary.service.impl;

import com.aliyun.heiyu.powermaster.server.rotary.dao.ModbusDAO;
import com.aliyun.heiyu.powermaster.server.rotary.dao.impl.ModbusDAOImpl;
import com.aliyun.heiyu.powermaster.server.rotary.object.entity.DModbusEntity;
import com.aliyun.heiyu.powermaster.server.rotary.service.ModbusService;
import org.apache.commons.collections.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description modbus业务接口实现类
 * @Author shun.zhang
 * @Date 2020/1/7 14:08
 **/
public class ModbusServiceImpl implements ModbusService {
    private ModbusDAO modbusDAO;

    public ModbusServiceImpl() {
        modbusDAO = new ModbusDAOImpl();
    }

    @Override
    public Map<String, String> getModbusMapByType() {
        List<DModbusEntity> modbusEntitys = modbusDAO.queryModbusList();
        if (CollectionUtils.isEmpty(modbusEntitys)) {
            return null;
        }
        Map<String, String> modbusMap = new HashMap<>();
        /**遍历集合组合成map*/
        modbusEntitys.forEach(modbusEntity -> {
            StringBuilder sbu = new StringBuilder();
            sbu.append(modbusEntity.getModbus_type())
                .append("_")
                .append(modbusEntity.getVar_address());
            modbusMap.put(sbu.toString(), modbusEntity.getVar_type());
        });
        return modbusMap;
    }
}
